package main

import (
	"fmt"
	"os/exec"
	"runtime"
	"runtime/pprof"
	"time"
)

// 控制线程数过多问题：https://blog.csdn.net/EDDYCJY/article/details/121026499
func main() {
	threadProfile := pprof.Lookup("threadcreate")
	fmt.Printf(" init threads counts: %d\n", threadProfile.Count())
	for i := 0; i < 20; i++ {
		go func() {
			runtime.LockOSThread() // 增加的一行代码
			_, err := exec.Command("bash", "-c", "sleep 3").Output()
			if err != nil {
				panic(err)
			}
		}()
	}
	time.Sleep(time.Second * 5)
	fmt.Printf("exit normally after 10 seconds, end threads counts: %d\n", threadProfile.Count())
}
